package com.hecore.lagou.mr.component;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

/**
 * @Author: hecore
 * @Date: 2020/10/18 17:29
 * @Description:
 *
 *  第一对Key,Value的输入 与Mapper的输出 一致。
 *
 */

public class WcReducer extends Reducer<Text, IntWritable, Text,IntWritable> {

    int sum;
    IntWritable total=new IntWritable();

    /**
     *
     * @param key  输入key
     * @param values  输入Value的迭代器
     * @param context  reducer上下文
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
        // 累加求和
        sum = 0;
        for (IntWritable count:
             values) {
            sum+=count.get();
        }
        // out
        total.set(sum);
        context.write(key,total);
    }

}
